package com.calsoft.Hops_Mobile_Tablet.sql;
/*
 * User: Minh Diep
 * Date: 7/8/13
 * Time: 2:12 PM
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import com.calsoft.Hops_Mobile_Tablet.base.App;

import java.io.IOException;

public class OpToursDbAdapter {

    private static final String TABLE = "opTours";
    public static final String ID = "_id";
    public static final String T_TYPE = "t_type";
    public static final String CITY = "city";
    public static final String LAND_SERVICE_CODE = "landServiceCode";
    public static final String SERVICE_TYPE = "serviceType";
    public static final String DISPLAY_NAME = "displayName";
    public static final String SERVICE_NAME = "serviceName";
    public static final String IMAGE_TAG = "imageTag";
    public static final String PRICE = "price";
    public static final String WISH_LIST = "wishList";

    public static final String[] selectedCols = {ID, T_TYPE, CITY, LAND_SERVICE_CODE, SERVICE_TYPE, DISPLAY_NAME, SERVICE_NAME, IMAGE_TAG, PRICE, WISH_LIST};

    private DataBaseHelper mDbHelper;
    private Context context;

    public OpToursDbAdapter (Context context){
        this.context = context;
    }

    public void open() throws SQLException {
        mDbHelper = new DataBaseHelper(context);
        try {
            mDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            mDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }
    }

    public void close() {
        mDbHelper.close();
    }

    public Cursor fetchAllServiceByLocation(String city, boolean[] selectedItems){

        String appendQuery = "";

        for (int i = 0; i < selectedItems.length; i++){
            if (selectedItems[i]){
                appendQuery += " or " + SERVICE_TYPE + "=\"" + App.TOURS_CATEGORIES[i] + "\"" ;
            }
        }

        if (appendQuery.length() > 0){
            appendQuery = appendQuery.substring(4);
            appendQuery = " and " + "(" + appendQuery + ")";
        } else {
            appendQuery = " and " + SERVICE_TYPE + "=\"" + "EMPTY" + "\"" ;
        }

        String t_type = "Non Schedule";
        if (city.length() == 0){
            return mDbHelper.queryRow(TABLE, selectedCols, T_TYPE + "=\"" + t_type + "\"" + appendQuery, SERVICE_TYPE);
        }
        return mDbHelper.queryRow(TABLE, selectedCols, CITY + "=\"" + city + "\"" + " and " + T_TYPE + "=\"" + t_type + "\"" + appendQuery, SERVICE_TYPE);
    }

    public int updateWishList(String landServiceCode, String yesNo){
        String strFilter =  LAND_SERVICE_CODE + "=\"" + landServiceCode + "\"";
        ContentValues args = new ContentValues();
        args.put(WISH_LIST, yesNo);
        return mDbHelper.update(TABLE, strFilter, args);
    }

    public Cursor fetchServiceByWishList(){
        return mDbHelper.queryRow(TABLE, selectedCols, WISH_LIST + "=\"" + "Y" + "\"", SERVICE_TYPE);
    }

    public Cursor fetchServiceByLandServiceCode(String code){
        return mDbHelper.queryRow(TABLE, selectedCols, LAND_SERVICE_CODE + "=\"" + code + "\"", null);
    }

}
